/**
 * kl yy-ds @2021
 */
package com.cxps.server.common.controller;

import com.cxps.cloud.message.ApiResponse;
import com.cxps.server.common.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 文件控制层
 *
 * @author YouCai.Liu
 * @since 2021/10/21
 * ...
 */
@Api(tags = "文件API")
@RestController
@RequestMapping("/admin/file")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 文件上传
     *
     * @param file
     */
    @PostMapping("/upload")
    @ApiOperation(value = "文件上传", response = ApiResponse.class)
    public ApiResponse upload(@RequestParam("file") MultipartFile file) throws IOException {
        Map<String, Object> map = fileService.upload(file);
        return ApiResponse.fireSuccess(map);
    }

    /**
     * 根据文件名下载文件
     *
     * @param fileName 服务上的文件名
     * @param response
     * @return
     */
    @GetMapping("/download")
    @ApiOperation(value = "根据文件名下载文件", response = ApiResponse.class)
    public void download(@RequestParam("fileName") String fileName, HttpServletResponse response) {
        if(StringUtils.isBlank(fileName)){
            return;
        }
        fileService.download(fileName, response);
    }


    /**
     * 文件上传
     *
     * @param file
     */
    @PostMapping("/uploadOss")
    @ApiOperation(value = "文件上传", response = ApiResponse.class)
    public ApiResponse uploadOss(@RequestParam("file") MultipartFile file) throws IOException {
        Map<String, Object> map = fileService.uploadOss(file);
        return ApiResponse.fireSuccess(map);
    }
}
